package com.gali.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Auther: gali
 * @Date: 2022-08-23 16:23
 * @Description:
 */
class Number{
    AtomicInteger atomicInteger=new AtomicInteger();
    public void add(){
        atomicInteger.getAndIncrement();
    }
}
public class AtomicIntegerDemo {
    public static final int SIZE=50;
    public static void main(String[] args) throws InterruptedException {
        Number number = new Number();
        CountDownLatch countDownLatch = new CountDownLatch(SIZE);
        for (int i = 1; i <=SIZE; i++) {
            new Thread(()->{
                try {
                    for (int j = 1; j <= 1000; j++) {
                        number.add();
                    }
                } finally {
                    countDownLatch.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t"+number.atomicInteger.get());
    }
}
